Type Jukebox

	Field credit:CreditManager
	Field tracks:TrackCollection
	Field playerBuilder:TrackPlayerBuilder
	Field player:TrackPlayer

	Field observers:TList

	Function Create:Jukebox(c:CreditManager, tpb:TrackPlayerBuilder)
		Local j:Jukebox = New Jukebox
		j.credit        = c
		j.tracks        = New TrackCollection
		j.playerBuilder = tpb
		j.player        = Null
		Return j
	End Function

	Method New()
		observers = New TList
	End Method
	
	Method addObserver(o:JukeboxObserver)
		If observers.Contains(Object(o)) = False Then
			observers.addLast(o)
		End If
	End Method
	
	Method removeObserver(o:JukeboxObserver)
		observers.Remove(Object(o))
	End Method
	
	Method clearObservers()
		observers.Clear()
	End Method	
	
	Method NotifyObservers()
		Local o:Object
		For o = EachIn observers
			JukeboxObserver(o).Update(Self)
		Next
	End Method

	Method addTrack:Int(t:TrackInformation)
		If credit.hasCredit() = False Then
			Return False
		End If
		tracks.AddLast(t)
		credit.removeCredit()
		If Not player Or Not player.IsPlaying() Then
			nextTrack()
		Else
			NotifyObservers()
		End If
		Return True
	End Method
	
	Method current:TrackPlayer()
		Return player
	End Method 
	
	Method nextTrack()
		If player Then
			player.stop()
		End If
		player = Null
		Local t:TrackInformation = tracks.First()
		If Not t Then
			Return
		End If
		tracks.Remove(t)
		player = playerBuilder.Build(t)
		player.play()
		NotifyObservers()
	End Method
	
End Type